#include "windoweditorfase.h"

#include <QPushButton>
#include <QDebug>
#include <QFileDialog>
#include <QDir>

WindowEditorFase::WindowEditorFase(QWidget *parent) :
    QMainWindow(parent),
    selecionaElementoToolbar(new QToolBar("Selecione um elemento para a fase", this)),
    actions(QMap<editacao::Elementos, QAction*>())
{
    this->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.523, y1:0, x2:0.511364, y2:1, stop:0 rgba(0, 0, 0, 120), stop:1 rgba(230, 235, 255, 255))");
    this->setWindowTitle("Editor de Fases");
    this->setWindowIcon(QIcon(":img/img/icon.png"));
    this->setGeometry(400, 200, 700, 500);

    this->montarCentralWidget();
    this->montarActions();
    this->montarToolBar();
    this->conectarMembros();
}

WindowEditorFase::~WindowEditorFase()
{
    delete this->labelImgFase;

    this->deletaActions();
    this->actions.clear();
}

void WindowEditorFase::onButtonSalvarClicked()
{
    QString path = QFileDialog::getSaveFileName(this, "Salvar Fase", QDir::currentPath(),"Fase Files (*.fas)");
    emit this->salvaFase(path);
}

void WindowEditorFase::montarCentralWidget()
{
    this->setCentralWidget(new QWidget(this));
    this->centralWidget()->setGeometry(100, 100, 700, 550);

    this->labelImgFase = new LabelImagemFase(this->centralWidget());

    this->layoutWidget = new QWidget(this->centralWidget());
    this->layoutWidget->setGeometry(20, 400, 600, 70);

    this->layoutWidget->setLayout(new QHBoxLayout(this->layoutWidget));
    this->layoutWidget->layout()->setSpacing(200);

    this->buttonUndo = new QPushButton("Undo", this->layoutWidget);
    this->buttonSalvar = new QPushButton("Salvar", this->layoutWidget);

    this->layoutWidget->layout()->addWidget(this->buttonUndo);
    this->layoutWidget->layout()->addWidget(this->buttonSalvar);

}

void WindowEditorFase::montarActions()
{
    this->actions[editacao::CAIXA] = new QAction(QIcon(":img/img/caixa.png"), "Caixa", this->selecionaElementoToolbar);
    this->actions[editacao::HEROI] = new QAction(QIcon(":img/img/spiderman.png"), "Heroi", this->selecionaElementoToolbar);
    this->actions[editacao::PAREDE] = new QAction(QIcon(":img/img/parede.png"), "Parede", this->selecionaElementoToolbar);
    this->actions[editacao::CHAO] = new QAction(QIcon(":img/img/chao.png"), "Chao", this->selecionaElementoToolbar);
    this->actions[editacao::PONTOX] = new QAction(QIcon(":img/img/pontox.png"), "Ponto X", this->selecionaElementoToolbar);
}

void WindowEditorFase::deletaActions()
{
    QMap<editacao::Elementos, QAction*>::iterator iterador;

    for(iterador = this->actions.begin(); iterador != this->actions.end(); ++iterador)
    {
        delete iterador.value();
    }
}

void WindowEditorFase::montarToolBar()
{
    this->addToolBar(Qt::LeftToolBarArea, this->selecionaElementoToolbar);
    this->selecionaElementoToolbar->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.523, y1:0, x2:0.511364, y2:1, stop:0 rgba(25, 180, 255, 120), stop:1 rgba(230, 235, 255, 255))");

    QMap<editacao::Elementos, QAction*>::iterator iterador;
    for(iterador = actions.begin(); iterador != actions.end(); ++iterador)
    {
        this->selecionaElementoToolbar->addAction(iterador.value());
        this->selecionaElementoToolbar->addSeparator();
    }

}

void WindowEditorFase::conectarMembros()
{
    this->connect(this->selecionaElementoToolbar, SIGNAL(actionTriggered(QAction*)), this->labelImgFase, SLOT(trocaElementoDeFase(QAction*)));
    this->connect(this->buttonSalvar, SIGNAL(clicked()), this, SLOT(onButtonSalvarClicked()));
    this->connect(this, SIGNAL(salvaFase(QString&)), this->labelImgFase, SLOT(salvaImagem(QString&)));
}
